//========================================================================
// *作者：#YSW#  邮箱：#AuthorEmail#
// *脚本：GameManager
// *时间：#CreateTime#
// *©2021 MYJL . All rights reserved.
//* ======================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using static UnityEditor.Progress;

namespace HDMCode.FrameEditor
{
    /// <summary>
    /// 获取的图标类型
    /// </summary>
    public enum IconType
    {
        /// <summary>
        /// 来源自Unity的图标
        /// </summary>
        UnityIcon,

        /// <summary>
        /// 来源自HDMFrame的图标
        /// </summary>
        HDMIcon,
    }



    public class HDMEditorHelper
    {
        /// <summary>
        /// 绘制多选UI
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public class DrawToolbar<T>
        {
            public bool initBool = false;
            public int barValue;
            private T[] outValue;
            public void DrawTool(string[] barTexts, T[] outValue,params GUILayoutOption[] guiOption)
            {
                this.outValue = outValue;
                barValue = GUILayout.Toolbar(barValue, barTexts, guiOption);
            }
            public void DrawTool(GUIContent[] style, T[] outValue,params GUILayoutOption[] guiOption)
            {
                this.outValue = outValue;
                barValue = GUILayout.Toolbar(barValue, style, guiOption);
            }

            public T DrawTool(T value, Dictionary<string,T> outValue, params GUILayoutOption[] guiOption)
            {
                if (!initBool)
                {
                    for (int i = 0; i < outValue.Count; i++)
                    {
                        var readDate = outValue.ElementAt(i);
                        if (Equals(value, readDate.Value))
                        {
                            barValue = i;
                            break;
                        }
                    }
                    initBool = true;
                }
                barValue = GUILayout.Toolbar(barValue,  outValue.Keys.ToArray() , guiOption);
                return outValue.ElementAt(barValue).Value;
            }

            public T GetValue() => outValue[barValue];
        }

        /// <summary>
        /// 绘制设置
        /// </summary>
        /// <param name="action"></param>
        public static void DrawSettingIcon(Action action) 
        {
            using (new GUILayout.HorizontalScope())
            {
                GUILayout.Space(20);
                GUILayout.FlexibleSpace();
                DrawButton("Setting.png", new[] { GUILayout.Width(25),GUILayout.Height(25)} , () => {
                    action?.Invoke();
                });;
                    
            }
            GUILayout.Space(-30);
        }

        /// <summary>
        /// 绘制视图弹窗提示
        /// </summary>
        /// <param name="tileName"> 弹出内容 </param>
        public static void DrawTip(string tileName)
        {
            HDMEditor.thisWindow.ShowNotification(new GUIContent(tileName));
        }

        /// <summary>
        /// 绘制弹窗提示
        /// </summary>
        /// <param name="tileName"> 弹窗标题 </param>
        /// <param name="content"> 弹窗内容 </param>
        /// <param name="verifyCallBack"> 确认按钮回调 </param>
        /// <param name="cancelCallBack"> 取消按钮回调 </param>
        public static void OpenWinTip(string tileName, string content, Action verifyCallBack = null, Action cancelCallBack = null)
        {
            if (EditorUtility.DisplayDialog(tileName, content, "确认", "取消"))
                verifyCallBack?.Invoke();
            else
                cancelCallBack?.Invoke();
        }

        /// <summary>
        /// 绘制弹窗提示
        /// </summary>
        /// <param name="tileName"> 弹窗标题 </param>
        /// <param name="content"> 弹窗内容 </param>
        /// <param name="verifyCallBack"> 确认按钮回调 </param>
        public static void OpenWinTip(string tileName, string content, Action verifyCallBack = null)
        {
            if (EditorUtility.DisplayDialog(tileName, content, "确认"))
                verifyCallBack?.Invoke();
        }

        /// <summary>
        /// 选中文件
        /// </summary>
        /// <param name="Path"> 需要选中的路径 </param>
        public static void SeletFile(string Path)
        {
            UnityEngine.Object obj = AssetDatabase.LoadMainAssetAtPath(Path);
            EditorGUIUtility.PingObject(obj);
            Selection.activeObject = obj;
        }

        /// <summary>
        /// 获取当前选中的文件夹
        /// </summary>
        public static string GetSleletPath() 
        {
            //获取选中文件夹的目录
            UnityEngine.Object target = Selection.activeObject;
            return AssetDatabase.GetAssetPath(target);
        }

        /// <summary>
        /// 绘制横行分割线
        /// </summary>
        public static void DrawXLine(int space=0)
        {
            GUILayout.Space(space);
            GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(2));
        }

        /// <summary>
        /// 绘制竖向分割线
        /// </summary>
        public static void DrawYLine()
        {
            GUILayout.Box("", HDMEditorStyle.styleYLine, GUILayout.Width(3), GUILayout.MaxWidth(3), GUILayout.MinWidth(3), GUILayout.ExpandHeight(true));
        }

        /// <summary>
        /// 绘制竖向进度条
        /// </summary>
        /// <param name="eventBack"> 创建完成回调 </param>
        public static void DrawYProgress(Action eventBack = null)
        {
            Rect assets = EditorGUILayout.BeginVertical(new[] { GUILayout.Width(400) });
            {
                assets.x = assets.x + 10;
                assets.y = assets.y + 5;
                GUI.Box(assets, "", EditorStyles.helpBox);
                GUILayout.Space(15);
                eventBack?.Invoke();
                Rect rectValue = assets;
                rectValue.width = 6f;
                EditorGUI.ProgressBar(rectValue, 1f, "");
            };
            EditorGUILayout.EndVertical();
        }

        /// <summary>
        /// 绘制通用标题
        /// </summary>
        /// <param name="tileName"> 标题名称 </param>
        public static void DrawTileLable(string tileName)
        {
            GUILayout.Space(10);
            GUILayout.Box(tileName, HDMEditorStyle.styleSubtitle, new[] { GUILayout.ExpandWidth(true) });
            GUI.color = Color.white;
            HDMEditorHelper.DrawXLine();
        }

        /// <summary>
        /// 绘制内容
        /// </summary>
        /// <param name="tileName"> 标题名称 </param>
        public static void DrawPagaTipLable(string tileName)
        {
            GUILayout.Label(tileName, HDMEditorStyle.styleTip, new[] { GUILayout.ExpandWidth(true) });
        }


        /// <summary>
        /// 绘制自适应按钮
        /// </summary>
        /// <param name="ButtonName"> 按钮名称 </param>
        /// <param name="buttonIcon"> 按钮图标 </param>
        /// <param name="buttonEvent"> 按钮事件 </param>
        public static void DrawButton(string ButtonName,string buttonIcon,Action buttonEvent = null) 
        {
            GUIContent button = new GUIContent($"    {ButtonName}");
            button.image = GetGUI(buttonIcon, IconType.HDMIcon);
            if (GUILayout.Button(button, HDMEditorStyle.styleButton, new[] { GUILayout.ExpandWidth(true), GUILayout.Height(30) }))
                buttonEvent?.Invoke();
        }

        /// <summary>
        /// 绘制自定义大小按钮
        /// </summary>
        /// <param name="ButtonName"> 按钮名称 </param>
        /// <param name="buttonIcon"> 按钮图标 </param>
        /// <param name="buttonEvent"> 按钮事件 </param>
        public static void DrawButton(string ButtonName, string buttonIcon , GUILayoutOption[] options , Action buttonEvent = null)
        {
            GUIContent button = new GUIContent($"    {ButtonName}");
            button.image = GetGUI(buttonIcon, IconType.HDMIcon);
            if (GUILayout.Button(button, HDMEditorStyle.styleButton, options ))
                buttonEvent?.Invoke();
        }

        /// <summary>
        /// 绘制自定义大小按钮
        /// </summary>
        /// <param name="ButtonName"> 按钮名称 </param>
        /// <param name="buttonIcon"> 按钮图标 </param>
        /// <param name="buttonEvent"> 按钮事件 </param>
        public static void DrawButton(string buttonIcon, GUILayoutOption[] options, Action buttonEvent = null)
        {
            GUIContent button = new GUIContent($"");
            button.image = GetGUI(buttonIcon, IconType.HDMIcon);
            if (GUILayout.Button(button, HDMEditorStyle.styleButton, options))
                buttonEvent?.Invoke();
        }

        /// <summary>
        /// 绘制Box
        /// </summary>
        /// <param name="lable">Box文本</param>
        /// <param name="iconName">Box图片</param>
        /// <param name="options">参数</param>
        public static void DrawBox(string lable,string iconName,GUIStyle style,params GUILayoutOption[] options) 
        {
            GUIContent box = new GUIContent(lable);
            box.image = GetGUI(iconName, IconType.HDMIcon);
            GUILayout.Box(box, style , options);
        }

        /// <summary>
        /// 绘制Lable
        /// </summary>
        /// <param name="lable">Box文本</param>
        /// <param name="iconName">Box图片</param>
        /// <param name="options">参数</param>
        public static void DrawLable(string lable, string iconName,GUIStyle style, params GUILayoutOption[] options)
        {
            GUIContent Lable = new GUIContent(lable);
            Lable.image = GetGUI(iconName, IconType.HDMIcon);
            GUILayout.Label(Lable, style, options);
        }

        /// <summary>
        /// 获取编辑器贴图
        /// </summary>
        /// <param name="imageName">图片名称</param>
        /// <param name="pathType"></param>
        /// <returns></returns>
        public static Texture GetGUI(string imageName, IconType pathType)
        {
            Texture icon = null;
            switch (pathType)
            {
                case IconType.HDMIcon:
                    icon = UnityEditor.AssetDatabase.LoadAssetAtPath<Texture>($"{HDMEditorDate.GUIIconFileAssetsPath}/{imageName}");
                    break;
                case IconType.UnityIcon:
                    icon = EditorGUIUtility.IconContent(imageName).image;
                    break;
            }
            return icon;
        }

        /// <summary>
        /// 创建一个配置文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="settingProjctPath">配置文件项目路径</param>
        /// <param name="settingAssetsPath">配置文件资源路径</param>
        /// <returns></returns>
        public static T CreatSetting<T>(string settingProjctPath , string settingAssetsPath) where T : ScriptableObject, new()
        {
            return HDMEditorMenuItem.CreatSetting<T>(settingProjctPath, settingAssetsPath);
        }

        /// <summary>
        /// 获取当前管理器窗口
        /// </summary>
        public static HDMEditor GetWindows() 
        {
            return HDMEditor.thisWindow;
        }

    }
}









